﻿using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using SemiCASE.Business;

public partial class UseCases : SemiCASEBasePage
{
    private string ProjectID
    {
        get
        {
            return Request.QueryString["ProjectID"] != null ? Request.QueryString["ProjectID"] : String.Empty;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            JavascriptHelper.PopUpWindowEventiEkle(HyperlinkNewUseCase, "~/PopUp/CreateUseCase.aspx?ProjectID=" + this.ProjectID + "&ScreenMode=Create", 800, 620, true);
            LoadUseCases();
            SetProjectName();
        }
    }

    private void SetProjectName()
    {
        LabelProjectName.Text = Session["ProjectName"] != null ? Session["ProjectName"].ToString().ToUpper() : String.Empty;
    }

    private void LoadUseCases()
    {
        if (String.IsNullOrEmpty(this.ProjectID))
            return;

        DataTable dt = new UseCaseBS().LoadUseCases(new Guid(this.ProjectID));

        for (int i = 0; i < dt.Rows.Count; i++)
        {
            dt.Rows[i]["SiraNo"] = (i + 1).ToString();
        }

        if (dt.Rows.Count > 0)
        {
            gvUseCases.DataSource = dt;
            gvUseCases.DataBind();
            LabelRowCount.Text = "Row Count: " + dt.Rows.Count.ToString();
            Session["UseCaseList"] = dt;
        }
        else
        {
            gvUseCases.DataSource = null;
            gvUseCases.DataBind();
            LabelRowCount.Text = String.Empty;
        }
    }

    protected void gvUseCases_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType != DataControlRowType.DataRow)
            return;

        string useCaseID = gvUseCases.DataKeys[e.Row.RowIndex].Values["UseCaseID"].ToString();
        string queryString = "?UseCaseID=" + useCaseID + "&ProjectID=" + this.ProjectID;

        HyperLink hyperLinkGuncelle = (HyperLink)e.Row.FindControl("HyperLinkUpdate");
        JavascriptHelper.PopUpWindowEventiEkle(hyperLinkGuncelle, "~/PopUp/CreateUseCase.aspx" + queryString + "&ScreenMode=Update", 800, 620, true);

        HyperLink hyperLinkDisplay = (HyperLink)e.Row.FindControl("HyperLinkDisplay");
        JavascriptHelper.PopUpWindowEventiEkle(hyperLinkDisplay, "~/PopUp/DisplayUseCase.aspx" + queryString, 800, 620, true);

        HyperLink hyperLinkTestCase = (HyperLink)e.Row.FindControl("HyperLinkTestCase");
        JavascriptHelper.PopUpWindowEventiEkle(hyperLinkTestCase, "~/PopUp/TestCaseList.aspx" + queryString, 800, 500, true);

        HyperLink hyperLinkWorkItems = (HyperLink)e.Row.FindControl("HyperLinkWorkItems");
        JavascriptHelper.PopUpWindowEventiEkle(hyperLinkWorkItems, "~/PopUp/WorkItemList.aspx" + queryString, 800, 500, true);
    }

    protected void gvUseCases_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "DeleteUseCase")
        {
            DeleteUseCase(e.CommandArgument.ToString());
        }
    }

    private void DeleteUseCase(string useCaseID)
    {
        new UseCaseBS().DeleteUseCase(new Guid(this.ProjectID), new Guid(useCaseID));
        LoadUseCases();
        MessageBox("Use Case deleted successfully", SemiCASE.Interfaces.MesajTuruEnum.Bilgi);
    }

    protected void gvUseCases_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvUseCases.PageIndex = e.NewPageIndex;
        DataTable dt = (DataTable)Session["UseCaseList"];
        gvUseCases.DataSource = dt;
        gvUseCases.DataBind();
    }

    protected void LinkButtonNavigateProjects_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Projects.aspx");
    }
}